﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;

namespace Storm.Docking.Visual.Drawing
{
    /// <summary>
    /// Since creating a new brush/pen everytime we need to
    /// redraw a TextView is too inefficient, we keep them in
    /// a stack here.
    /// </summary>
    public class BrushStack
    {
        #region Fields

        private static Hashtable brushes = new Hashtable();
        private static Hashtable pens = new Hashtable();

        #endregion

        #region Methods

        #region Public

        /// <summary>
        /// Returns a Brush from the stack of brushes.
        /// If the brush stack doesn't contain the specified
        /// Brush, creates a Brush and adds it to the stack.
        /// </summary>
        /// <param name="color">Color of the returned Brush.</param>
        /// <returns>Brush from the stack of brushes.</returns>
        public static Brush GetBrush(Color color)
        {
            if (brushes.Contains(color) == false)
            {
                Brush brush = new SolidBrush(color);
                brushes.Add(color, brush);
                return brush;
            }

            return brushes[color] as Brush;
        }

        /// <summary>
        /// Returns a Pen from the stack of pens.
        /// If the pen stack doesn't contain the specified
        /// Pen, creates a Pen and adds it to the stack.
        /// </summary>
        /// <param name="color">Color of the returned Pen.</param>
        /// <returns>Pen from the stack of pens.</returns>
        public static Pen GetPen(Color color)
        {
            if (pens.Contains(color) == false)
            {
                Pen pen = new Pen(color);
                pens.Add(color, pen);
                return pen;
            }

            return pens[color] as Pen;
        }

        #endregion

        #endregion
    }
}
